//
//  YLLTabBar.swift
//  YLLSina
//
//  Created by 于玲玲 on 16/7/28.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class YLLTabBar: UITabBar {

    //声明一个闭包
    var closure:(()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(compose2Button)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = bounds.width / 5
        let height = bounds.height
        
        var index = 0
        
        for subview in subviews{
            //为啥有叹号
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRectMake(CGFloat(index) * width, bounds.origin.y, width, height)
                
                // 修改索引的值
                index++
                
                // 判断一下索引,然后空出中间的位置
                if index == 2 {index++}

            }
            
        }
        // 设置中间加号按钮的位置
        compose2Button.frame = CGRectMake(2 * width, bounds.origin.y, width, height)
        
    }
    // MARK:-- 按钮的点击方法
    func click(){
        
//        print("点击了")
        closure?()
    }

    lazy var composeButton: UIButton = UIButton()
    
    lazy var compose2Button: UIButton = {
        
        let button = UIButton()
        
        // 设置属性
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加点击事件
        button.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button

        
    }()

}
